package org.hbgl.geoentity.db.vo;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import org.apache.commons.collections4.CollectionUtils;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 代码功能：
 * 开发日期：2022/10/12
 * 作者：蒋海仑
 * 联系方式：
 * 版本更迭说明：
 * 版权信息：河北广联信息技术有限公司
 */
@Data
public class ColumnBoxVo implements Serializable {

    private Integer id;
    private Integer parentId;
    private String name;
    private Integer state;
    private Integer sort;
    private LocalDateTime addTime;
    private LocalDateTime editTime;

    private String parentName;

    private List<ColumnBoxVo> children;

    /**
     * 使⽤递归⽅法建树
     *
     * @param list 行政区划列表
     * @return 树
     */
    public static List<ColumnBoxVo> build(List<ColumnBoxVo> list) {
        List<ColumnBoxVo> trees = new ArrayList<>();
        if (CollectionUtils.isEmpty(list)) {
            return trees;
        }
        Integer pidMin = list.stream().mapToInt(ColumnBoxVo::getParentId).summaryStatistics().getMin();
        List<ColumnBoxVo> listMin = list.stream().filter(p -> p.getParentId().equals(pidMin)).collect(Collectors.toList());
        for (ColumnBoxVo item : listMin) {
            ColumnBoxVo parent = new ColumnBoxVo();
            parent.setId(item.getId());
            parent.setParentId(item.getParentId());
            parent.setName(item.getName());
            parent.setSort(item.getSort());
            findChild(parent, list);
            trees.add(parent);
        }
        return trees;
    }

    private static ColumnBoxVo findChild(ColumnBoxVo parent, List<ColumnBoxVo> list) {
        for (ColumnBoxVo item : list) {
            ColumnBoxVo child = new ColumnBoxVo();
            child.setId(item.getId());
            child.setParentId(item.getParentId());
            child.setName(item.getName());
            child.setSort(item.getSort());
            if (parent.getId().equals(child.getParentId())) {
                if (parent.getChildren() == null) {
                    parent.setChildren(new ArrayList<>());
                }
                parent.getChildren().add(findChild(child, list));
            }
        }
        return parent;
    }

    /**
     * 使⽤递归⽅法建树
     *
     * @param list 行政区划列表
     * @return 树
     */
    public static List<ColumnBoxVo> buildDRev(Integer childId, List<ColumnBoxVo> list) {
        List<ColumnBoxVo> trees = new ArrayList<>();
        if (CollectionUtils.isEmpty(list)) {
            return trees;
        }
        //获取父级栏目
        List<ColumnBoxVo> collect = list.stream().filter(p -> p.getId().equals(childId)).collect(Collectors.toList());
        ColumnBoxVo parent = collect.get(0);
        if(collect.get(0).getParentId()!=0) {
            parent = findParent(collect, list);
        }

        trees.add(parent);
        return trees;
    }

    private static ColumnBoxVo findParent(List<ColumnBoxVo> child, List<ColumnBoxVo> list) {
        List<ColumnBoxVo> collect = list.stream().filter(p -> p.getId().equals(child.get(0).getParentId())).collect(Collectors.toList());
        ColumnBoxVo columnBoxVo1 = collect.get(0);
        List<ColumnBoxVo> columnBoxList = new ArrayList<ColumnBoxVo>();
        ColumnBoxVo columnBoxVo = new ColumnBoxVo();
        columnBoxVo.setId(columnBoxVo1.getId());
        columnBoxVo.setParentId(columnBoxVo1.getParentId());
        columnBoxVo.setName(columnBoxVo1.getName());
        columnBoxVo.setState(columnBoxVo1.getState());
        columnBoxVo.setSort(columnBoxVo1.getSort());
        columnBoxVo.setAddTime(columnBoxVo1.getAddTime());
        columnBoxVo.setEditTime(columnBoxVo1.getEditTime());
        columnBoxVo.setParentName(columnBoxVo1.getParentName());
        columnBoxVo.setChildren(child);
        if(columnBoxVo.getParentId()==0) {
            columnBoxList.add(columnBoxVo);
            return columnBoxVo;
        }else {
            columnBoxList.add(columnBoxVo);
            ColumnBoxVo parent = findParent(columnBoxList, list);
            return parent;
        }
    }

    /**
     * 使⽤递归⽅法建树
     *
     * @param list 行政区划列表
     * @return 树
     */
    public static List<ColumnBoxVo> buildUp(Integer columnId, List<ColumnBoxVo> list) {
        List<ColumnBoxVo> trees = new ArrayList<>();
        if (CollectionUtils.isEmpty(list)) {
            return trees;
        }
        list = list.stream().filter(p -> !p.getId().equals(columnId)).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(list)) {
            return trees;
        }
        Integer pidMin = list.stream().mapToInt(ColumnBoxVo::getParentId).summaryStatistics().getMin();
        List<ColumnBoxVo> listMin = list.stream().filter(p -> p.getParentId().equals(pidMin)).collect(Collectors.toList());
        for (ColumnBoxVo item : listMin) {
            ColumnBoxVo parent = new ColumnBoxVo();
            parent.setId(item.getId());
            parent.setParentId(item.getParentId());
            parent.setName(item.getName());
            parent.setSort(item.getSort());
            findChild(parent, list);
            trees.add(parent);
        }
        return trees;
    }

}
